package mainpackage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class HOSMenu {

	private static HOS hos;

	public static void main(String[] args) throws Exception {

		System.out.println("----------------------------------------------");
		System.out.println("Hotel Booking System - Loading data");
		System.out.println("----------------------------------------------");
		String itemfile, roomfile;
		/* input data from terminal */
		Scanner scanner = new Scanner(System.in);
		System.out
				.println("Enter the name for ITEMS data file (including the extension):");
		itemfile = scanner.next();
		System.out
				.println("Enter the name for ROOMS data file (including the extension):");
		roomfile = scanner.next();

		hos = new HOS();
		hos.loadItemList(itemfile);
		hos.loadRoomList(roomfile);

		String flag;
		while (true) {
			flag = displayMainMenu();
			switch (flag) {
			case "1":
				setFilter();
				break;
			case "2":
				createNewOrder();
				break;
			case "3":
				addItem();
				break;
			case "4":
				removeItem();
				break;
			case "5":
				displayOrder();
				break;
			case "6":
				finalizeOrder();
				break;

			case "S":
				displayState();
				break;
			case "E":
				System.out.println("system exit...");
				System.exit(0);

			default:
				break;
			}
		}
	}

	private static void displayState() {
		hos.displayState();
	}

	private static void finalizeOrder() throws IOException {
		System.out.println("----------------------------------------------");
		System.out.println("Hotel Booking System - Finalize Order");
		System.out.println("----------------------------------------------");
		System.out.println("Room Number:  "
				+ hos.getCurrentOrder().getRoom().getRoomNr());
		System.out.println("Food items ordered:");
		double total = 0;
		for (int i = 0; i < hos.getCurrentOrder().getItems().size(); i++) {
			System.out.println("    "
					+ hos.getCurrentOrder().getItems().get(i).getName() + " $"
					+ hos.getCurrentOrder().getItems().get(i).getPrice());
			total += hos.getCurrentOrder().getItems().get(i).getPrice();
		}
		System.out.println("Total: $" + total);
		System.out.print("Do you want to conintue?(y/n):");
		Scanner scanner = new Scanner(System.in);
		String s;
		s = scanner.next();
		switch(s){
			case "y":
				hos.saveOrder();
				System.out.println("The order has been saved!");
				hos.setCurrentOrderNull();
				System.exit(0);
			case "Y":
				hos.saveOrder();
				System.out.println("The order has been saved!");
				hos.setCurrentOrderNull();
				System.exit(0);
			case "n":
				hos.saveOrder();
				System.out.println("The order has been cancelled!");
				hos.setCurrentOrderNull();
				System.exit(0);
			case "N":
				hos.saveOrder();
				System.out.println("The order has been cancelled!");
				hos.setCurrentOrderNull();
				System.exit(0);
			
			default:
				System.out.println("Your answer is invalid.No action is taken!");
				break;
		}
	}

	private static void displayOrder() {
		System.out.println("----------------------------------------------");
		System.out.println("Hotel Booking System - Display the Current Order");
		System.out.println("----------------------------------------------");
		System.out.println("Room Number:  "
				+ hos.getCurrentOrder().getRoom().getRoomNr());
		System.out.println("Food items ordered:");
		double total = 0;
		for (int i = 0; i < hos.getCurrentOrder().getItems().size(); i++) {
			System.out.println("    "
					+ hos.getCurrentOrder().getItems().get(i).getName() + " $"
					+ hos.getCurrentOrder().getItems().get(i).getPrice());
			total += hos.getCurrentOrder().getItems().get(i).getPrice();
		}
		System.out.println("Total: $" + total);
	}

	private static void removeItem() {
		System.out.println("----------------------------------------------");
		System.out
				.println("Hotel Booking System - Remove an Item from the Order");
		System.out.println("----------------------------------------------");
		Order currentorder = hos.getCurrentOrder();
		int i;
		for (i = 0; i < currentorder.getItems().size(); i++) {
			System.out.println((i + 1) + " : "
					+ currentorder.getItems().get(i).getName() + " "
					+ currentorder.getItems().get(i).getPrice());
		}
		System.out.println("----------------------------------------------");
		System.out
				.println("Enter your choice of item to remove (1-" + i + "):");
		Scanner scanner = new Scanner(System.in);
		int choice;
		choice = Integer.parseInt(scanner.next());
		hos.removeItemFromOrder(choice - 1);
	}

	private static void addItem() {
		// add item to currentOrder
		System.out.println("----------------------------------------------");
		System.out.println("Hotel Booking System - Add an Item to the Order");
		System.out.println("----------------------------------------------");
		ArrayList<Item> itemlist;
		itemlist = hos.getFilteredItemList();
		if(itemlist.size() == 0){
			System.out.println("filtered item list is null, please set another filter!\n");
			return ;
		}
		int i;
		for (i = 0; i < itemlist.size(); i++) {
			System.out.println((i + 1) + " : " + itemlist.get(i).getName()
					+ " " + itemlist.get(i).getPrice());
		}
		System.out.println("----------------------------------------------");
		System.out.println("Enter your choice of item to add (1-" + i + "):");
		int choice;
		Scanner scanner = new Scanner(System.in);
		choice = Integer.parseInt(scanner.next());
		
		hos.addItemToOrder(hos.getFilteredItemList().get(choice - 1));
	}

	private static void createNewOrder() {
		/* add information in the currentOrder */
		System.out.println("----------------------------------------------");
		System.out.println("Hotel Booking System - Create New Order");
		System.out.println("----------------------------------------------");
		System.out.println("Enter the room number:");
		int roomNr;
		Scanner scanner = new Scanner(System.in);
		roomNr = scanner.nextInt();
		hos.createNewOrder(roomNr);
	}

	private static void setFilter() {
		/* set filter in HOS */
		System.out.println("----------------------------------------------");
		System.out.println("Hotel Booking System - Set Filter");
		System.out.println("----------------------------------------------");
		System.out.println("1: ENTREE");
		System.out.println("2: MAIN_COURSE");
		System.out.println("3: SIDE_DISH");
		System.out.println("4: DESERT");
		System.out.println("5: DRINK");
		System.out.println("A: All (remove filter)");
		System.out.println("----------------------------------------------");
		System.out.println("Enter your option (1-5 or A):");
		String s;
		Scanner scanner = new Scanner(System.in);
		s = scanner.next();
		switch (s) {
		case "1":
			hos.setFilter(Category.ENTREE);
			break;
		case "2":
			hos.setFilter(Category.MAIN_COURSE);
			break;
		case "3":
			hos.setFilter(Category.SIDE_DISH);
			break;
		case "4":
			hos.setFilter(Category.DESERT);
			break;
		case "5":
			hos.setFilter(Category.DRINK);
			break;
		case "A":
			hos.setFilter(null);
			break;
		}

	}

	private static String displayMainMenu() {
		String s;
		System.out.println("----------------------------------------------");
		System.out.println("Hotel Booking System - Main Menu");
		System.out.println("----------------------------------------------");
		System.out.println("1: Set (ro remove) Filter");
		System.out.println("2: Create a New Order");
		System.out.println("3: Add an Item to the Current Order");
		System.out.println("4: Remove an Item from the Current Order");
		System.out.println("5: Display the Current Order");
		System.out.println("6: Finalize the Order\n");
		System.out.println("S: Show the state of the system for inspection");
		System.out.println("E: Exit");
		System.out.println("----------------------------------------------");
		System.out.println("Enter your option (1-6 or S or E):");
		Scanner scanner = new Scanner(System.in);
		s = scanner.next();
		return s;
	}
}
